//
//  ScoreController.swift
//  SchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit

class ScoreController: HSBasicController {
    
    var scroreVM = HSScoreViewModal()
    
//    fileprivate  var dataSouce:[HSScoreModal] = []
    fileprivate var rowHeight:CGFloat?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //注册原形cell
        tableView.register(ScoreTableViewCell.self, forCellReuseIdentifier: "scoreCell")
        
        scoreloadData()
        tableView.estimatedRowHeight = 100
        
    }
    //请求数据
    func scoreloadData() {
        scroreVM.requestHomeData {[weak self] (err, isRearsh) in
            if err != nil {
                return
            }
            if isRearsh {
                self?.tableView.reloadData()
            }
        }
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return scroreVM.scoreArr.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexpath:IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "scoreCell", for: indexpath) as! ScoreTableViewCell
        cell.selectionStyle = .none
        
        
        //赋值
        let modal = scroreVM.scoreArr[indexpath.row]
        cell.model =  modal
        cell.scoretitleL.text = modal.es_title
        cell.scoreNameBtn.setTitle(modal.es_course, for: .normal)
        cell.scoreTimeL.text = modal.es_createDate
        cell.scoreTeacherL.text = modal.es_teacherName
        cell.messageRemarL.text = modal.es_comment
        cell.scoreNumberL.text = "\(modal.es_score)"
        if modal.es_score >= 60 {
            cell.scoreView.image = UIImage(named: "scores_ok")
            cell.qualifiedL.text = "合格"
        } else {
            cell.scoreView.image = UIImage(named: "scores_ng")
            cell.qualifiedL.text = "不合格"
        }
        return cell
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        guard let rowHeight = rowHeight else {
            return 80
        }
        return rowHeight
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = scroreVM.scoreArr[indexPath.row]
        if model.cellIsSelected == false{
            model.cellIsSelected = true
        }else{
            model.cellIsSelected = false
        }
        tableView.reloadData()
        
    }
    
}


